﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmDanhhieu : Form
    {
        public frmDanhhieu()
        {
            InitializeComponent();
        }
        cls_DM_Danhhieu objDanhhieu = new cls_DM_Danhhieu();
        private void frmDanhhieu_Load(object sender, EventArgs e)
        {
            GetAll_Danhhieu();
        }
        private void GetAll_Danhhieu()
        {
            gridControl1.DataSource = objDanhhieu.GetAll_Danhhieu();
        }
        private void Save_Danhhieu()
        {
            clsDM_Danhhieu objdmdh = new clsDM_Danhhieu();
            objdmdh.TenDanhhieu = gridView1.GetFocusedRowCellValue("TenDanhHieu").ToString();
            objdmdh.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmdh.DanhHieuID = 1;
                objdmdh.isChangeable = false;
                blnResult = objDanhhieu.Insert(objdmdh);
            }
            else
            {
                objdmdh.DanhHieuID = (int)gridView1.GetFocusedRowCellValue("DanhHieuID");
                objdmdh.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objDanhhieu.update(objdmdh);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Danhhieu();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenDanhHieu").ToString()) == DialogResult.No)
                return;
            objDanhhieu.delete((int)gridView1.GetFocusedRowCellValue("DanhHieuID"));
            GetAll_Danhhieu();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Danhhieu();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }

    }
}
